﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Security.Policy;

namespace Macds.Core.ServiceLocator
{
    public class LocatorTarget
    {
        public LocatorTarget(string assemblyName, string className)
        {
            AssemblyName = assemblyName;
            ClassName = className;
            LoadType = TargetType.Assembly;
        }

        public LocatorTarget(string assemblyName, string className, object[] constructorParams)
        {
            AssemblyName = assemblyName;
            ClassName = className;
            LoadType = TargetType.Assembly;
            ConstructorParams = constructorParams;
        }

        public LocatorTarget(Type typeOf)
        {
            TypeOf = typeOf;
            LoadType = TargetType.TypeOf;
        }

        public LocatorTarget(Type typeOf, object[] constructorParams)
        {
            TypeOf = typeOf;
            LoadType = TargetType.TypeOf;
            ConstructorParams = constructorParams;
        }

        public string AssemblyName { get; private set; }
        public string ClassName { get; private set; }
        public Type TypeOf { get; private set; }
        public TargetType LoadType { get; private set; }
        public object[] ConstructorParams { get; private set; }
        public bool HasConstructorParams
        {
            get
            {
                return ConstructorParams != null && ConstructorParams.Length > 0;
            }
        }

        public object LoadObject()
        {
            if (LoadType == TargetType.Undefined)
                throw new ArgumentNullException("the LoadType is undefined");

            if (LoadType == TargetType.Assembly)
            {
                if (AssemblyName == null || AssemblyName == "")
                    throw new ArgumentNullException("the AssemblyName is null");

                if (ClassName == null || ClassName == "")
                    throw new ArgumentNullException("the ClassName is null");

                if(!HasConstructorParams) 
                {
                    return Activator.CreateInstance(AssemblyName, ClassName).Unwrap();
                }
                else
                {
                    var assembly = Assembly.Load(AssemblyName);
                    Type objectType = assembly.GetType(ClassName);
                    return Activator.CreateInstance(objectType, ConstructorParams);
                }
            }
            else
            {
                if (TypeOf == null)
                    throw new ArgumentNullException("the TypeOf is null");

                if(!HasConstructorParams)
                {
                    return Activator.CreateInstance(TypeOf);
                }
                else
                {
                    return Activator.CreateInstance(TypeOf, ConstructorParams);
                }
            }
        }
    }
}
